/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author THANH
 */
public class MyTableModel extends AbstractTableModel {

    private boolean DEBUG = false;
    Vector col = new Vector();
    Vector data = new Vector();


    /* ADDED MORE */
    public void insertcol(Vector c) {
        col = c;
    }

    public void insertRow(Vector vt) {
        data = vt;
    }

    public int getColumnCount() {
        return col.size();
    }

    public int getRowCount() {

        return data.size();
    }

    @Override
    public String getColumnName(int co) {
        return col.get(co).toString();
    }

    public Object getValueAt(int row, int col) {
        //return data[row][col];
        Vector c = (Vector) data.get(row);

        return c.get(col);

    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    @Override
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    @Override
    public void setValueAt(Object value, int row, int col) {
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                    + " to " + value
                    + " (an instance of "
                    + value.getClass() + ")");
        }

        //data[row][col] = value;
        Vector c = (Vector) data.get(row);
        c.set(col, value);

        data.set(row, c);

        fireTableCellUpdated(row, col);

        if (DEBUG) {
            System.out.println("New value of data:");
            printDebugData();
        }
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i = 0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j = 0; j < numCols; j++) {
                System.out.println(getValueAt(i, j));
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }

    public Vector getDataSelect() {
        Vector d = new Vector();
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i = 0; i < numRows; i++) {

            if ((Boolean) getValueAt(i, numCols - 1)) {
                d.add(getValueAt(i, 0).toString());
            }

        }
        return d;
    }

    public Vector getAllData() {
        Vector all = new Vector();
        int numRows = getRowCount();
        int numCols = getColumnCount();
        for (int i = 0; i < numRows; i++) {
            Vector d = new Vector();
            d.add(getValueAt(i, 0).toString());
            d.add(getValueAt(i, numCols - 1));
            all.add((d));
        }
        return all;
    }
}
